//下面用于图片上传预览功能
function setImagePreview(avalue) {
var docObj=document.getElementById("patPictureFile");
 
var imgObjPreview=document.getElementById("preview");
if(docObj.files &&docObj.files[0])
{
//火狐下，直接设img属性
imgObjPreview.style.display = 'block';
imgObjPreview.style.width = '150px';
imgObjPreview.style.height = '180px'; 
//imgObjPreview.src = docObj.files[0].getAsDataURL();
 
//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
}
else
{
//IE下，使用滤镜
docObj.select();
var imgSrc = document.selection.createRange().text;
var localImagId = document.getElementById("localImag");
//必须设置初始大小
localImagId.style.width = "150px";
localImagId.style.height = "180px";
//图片异常的捕捉，防止用户修改后缀来伪造图片
try{
localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
}
catch(e)
{
alert("您上传的图片格式不正确，请重新选择!");
return false;
}
imgObjPreview.style.display = 'none';
document.selection.empty();
}
return true;
}

/*************************************************************************************************
 * 							界面操作
 * ***********************************************************************************************/
 
var pictureUrl = null;//当前图片路径 如：123456.png
var pictureBeans = [];//
var css3Transform = null;
var picture_index = 0;

/**
 * 查询载入图片列表
 **/
function uploadPicture()
{
	var mrn = $("#picture_mrn").val();
	var p_date_String = $("#p_date_String").val();
	var memo = $("#picture_memo").val();
	if(mrn =="")
	{
		messageAlert(2,"请输入病历号！",function()
				{
			this.close();
			choose_close();
		});
		
		return;
	}
	if(!findPatByMrn(mrn))
	{
		messageAlert(2,"请输入正确的病历号！",function()
				{
			this.close();
			choose_close();
		});
		
		return;
	}
	if(p_date_String == "")
	{
		messageAlert(2,"请选择图像日期！",function()
				{
			this.close();
			choose_close();
		});
		
		return;
	}
	var url = projectName+"/picture/savePhoto.do?date=" + new Date().getTime();
	$.ajaxFileUpload({
		url:url,
		secureuri:false,
		data:{
			"mrn":mrn,
			"p_date_String":p_date_String,
			"memo": memo
		},
		type:"post",
		fileElementId:"patPictureFile",
		success:function(data,status){
			if(data.body.textContent == "SUCCESS")
			{
				$("#p_date_String").val("");
				$("#picture_memo").val("");
				messageAlert(1,"上传成功",function()
						{
					this.close();
					choose_close();
					showPictureInfo();
				});
			}
			else
			{
				messageAlert(3,"上传失败！请重试",function()
						{
					this.close();
					choose_close();
				});
			}
			
			
		},
		error: function(error){
			alert(error)
		}
	})
}

/**
 * 初始化日期 隐藏图片DIV
 **/
function initPictureDate(mrn)
{
	
	$("#p_date_String").css({cursor: 'pointer'});//手型光标
    $("#p_date_String").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    
    getPictureBeans(mrn);
    
	var Sys = {};
	var ua = navigator.userAgent.toLowerCase();
	var s;
	(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
	(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
	(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
	(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
	(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
	//以下进行测试
	if (Sys.ie) css3Transform = "-ms-transform";
	if (Sys.firefox) css3Transform = "-moz-transform";
	if (Sys.chrome) css3Transform = "-webkit-transform";
	if (Sys.opera) css3Transform = "-o-transform";
	if (Sys.safari) css3Transform = "-webkit-transform";
	
	var mid_height = $("#pictureMid").height();
	var mid_width = $("#pictureMid").width();
	
	var mid_top = ($(document).height() - mid_height) / 2 + "px";
	var mid_left = ($(document).width() - mid_width) / 2 + "px";
	$("#pictureMid").css({"display":"none" , "top":mid_top , "left":mid_left});//当前窗口中间位置
	
	var _move=false;//移动标记  
	var _x,_y;//鼠标离控件左上角的相对位置  
    $("#pictureMid").click(function(){  
        //点击（松开后触发）  
        }).mousedown(function(e){  
        _move=true;  
        _x=e.pageX-parseInt($("#pictureMid").css("left"));  
        _y=e.pageY-parseInt($("#pictureMid").css("top"));  
    });  
    $(document).mousemove(function(e){  
        if(_move){  
            var x=e.pageX-_x;//移动时根据鼠标位置计算控件左上角的绝对位置  
            var y=e.pageY-_y;  
            $("#pictureMid").css({top:y,left:x});//控件新位置  
        }  
    }).mouseup(function(){  
    _move=false;  
    });
    
    $("#pictureMid").css("cursor","pointer");
//	$('body').click(function(event) {
//		$("#pictureMid").css("display","none");
//        event.stopPropagation();
//	});
    initPictureClick();
}

/**
 * ajax获取图片列表
 **/
function getPictureBeans(mrn)
{
	var url = projectName + "/picture/getPictureBeans.do?date=" + new Date().getTime();
	$.ajax({
		url: url,
		data: {
			"mrn":mrn
		},
		async: false,
		success: function(data,result)
		{
			if(null != data)
			{
				pictureBeans = data;
			}
		}
	});
}

/**
 * jsp显示图片列表
 **/
function showPictureInfo()
{
	var mrn = $("#picture_mrn").val();
	if(mrn =="")
	{
		messageAlert(2,"请输入病历号！",function()
				{
			this.close();
			choose_close();
		});
		
		return;
	}
	if(!findPatByMrn(mrn))
	{
		messageAlert(2,"请输入正确的病历号！",function()
				{
			this.close();
			choose_close();
		});
		
		return;
	}
	
	window.location.href = projectName + "/picture/getPatPictureList.do?mrn=" + mrn;
}

/**
 * 点击查看图片
 **/
function look_picture(id)
{
	$("#pictureMid").css("display","block");
    //获取当前图片URL
    var obj = getPictureInfo(id);
    if(undefined != obj || null != obj)
    {
    	pictureUrl = "/patPicture/" + obj.picture;
        $("#mid_picture").attr("src",pictureUrl);
    }
    
    picturePosition();
}

/**
 * 	label绑定事件
 **/
function initPictureClick()
{
	var multiple = 1.1;
	var min_size = 20;
	var angle = 0;
	
	//放大
    $("#p_toBig").click(function(){
    	var max_size = $("#pictureMid").height() - 40;
    	var p_obj = $("#mid_picture");
    	var xz = (angle/90) % 2;
    	if(p_obj.height()*multiple >= max_size && xz == 0)
    	{
    		alert("已经最大了哦~")
    		return;
    	}
    	else if(p_obj.width()*multiple >= max_size && xz != 0)
    	{
    		alert("已经最大了哦~")
    		return;
    	}
    	else
    	{
    		p_obj.height(p_obj.height()*multiple);
        	p_obj.width(p_obj.width()*multiple);
        	picturePosition();
        	p_obj.attr("src",pictureUrl);
    	}
    });
    
    //缩小
    $("#p_toSmall").click(function(){
    	var p_obj = $("#mid_picture");
    	
    	if(p_obj.height()/multiple <= min_size)
    	{
    		alert("已经最小了哦~")
    	}
    	else
    	{
    		p_obj.height(p_obj.height()/multiple);
        	p_obj.width(p_obj.width()/multiple);
        	picturePosition();
        	p_obj.attr("src",pictureUrl);
    	}
    });
    
    //向右旋转
    $("#p_toTurnRight").click(function(){
    	if(!allowsRotation())
    	{
    		alert("请缩小图片再进行旋转操作！")
    		return;
    	}
    	angle += 90;
    	if(angle >= 360)
    	{
    		angle -= 360;
    	}
    	var angleString = "rotate(" + angle + "deg)";
    	$("#mid_picture").css(css3Transform,angleString);
    	$("#midend").css(css3Transform,angleString);
    });
    
    //向左旋转
    $("#p_toTurnLeft").click(function(){
    	if(!allowsRotation())
    	{
    		alert("请缩小图片再进行旋转操作！")
    		return;
    	}
    	angle -= 90;
    	if(angle <= -360)
    	{
    		angle += 360;
    	}
    	var angleString = "rotate(" + angle + "deg)";
    	$("#mid_picture").css(css3Transform,angleString);
    	$("#midend").css(css3Transform,angleString);
    });
    
    $("#p_toLast").click(function(){
    	if(picture_index == 0)
    	{
    		alert("这是第一张！")
    	}
    	else
    	{
    		var obj = pictureBeans[picture_index - 1];
    		if(undefined != obj || null != obj)
    		{
    			pictureUrl = "/patPicture/" + obj.picture;
    			$("#mid_picture").attr("src",pictureUrl);
    			picture_index--;
    		}
    	}
    });
    
    $("#p_toNext").click(function(){
    	if(picture_index == pictureBeans.length - 1)
    	{
    		alert("已到最后一张！")
    	}
    	else
    	{
    		var obj = pictureBeans[picture_index + 1];
    		if(undefined != obj || null != obj)
    		{
    			pictureUrl = "/patPicture/" + obj.picture;
    			$("#mid_picture").attr("src",pictureUrl);
    			picture_index++;
    		}
    	}
    });
    
    //退出
    $("#p_toExit").click(function(){
    	$("#pictureMid").hide();
    });
}

/**
 * 	判断图片是否允许旋转
 **/
function allowsRotation()
{
	var mid_height = $("#pictureMid").height();
	var mid_width = $("#pictureMid").width();
	var height = $("#mid_picture").height();
	var width = $("#mid_picture").width();
	if(width > mid_height || height > mid_width)
	{
		return false;
	}
	return true;
}

/**
 * 	图片居中显示
 **/
function picturePosition()
{
	var mid_height = $("#pictureMid").height();
	var mid_width = $("#pictureMid").width();
	var height = $("#mid_picture").height();
	var width = $("#mid_picture").width();
	var p_top = (mid_height - height) / 2;
	var p_left = (mid_width - width) / 2;
	$("#mid_picture").css({top:p_top,left:p_left});
    $("#midend").css({top:p_top,left:p_left});
}

/**
 * 	根据ID获取picture信息
 **/
function getPictureInfo(id)
{
	var obj = null;
	for(var i = 0; i < pictureBeans.length; i++)
	{
		if(id == pictureBeans[i].id)
		{
			obj = pictureBeans[i];
			picture_index = i;
			return obj;
		}
	}
//	var url = projectName + "/picture/getPictureById.do?date=" + new Date().getTime();
//	$.ajax({
//		url: url,
//		async: false,
//		data:{
//			"id": id
//		},
//		success: function(data,result)
//		{
//			obj = data;
//		}
//	});
	return obj;
}

function del_picture(id)
{
	var url = projectName + "/picture/deletePictureById.do?date=" + new Date().getTime();
	$.ajax({
		url:url,
		data:{
			"id":id
		},
		async:false,
		success:function(data,result){
			if("SUCCESS" == data)
			{
				messageAlert(1,"删除成功！",function()
						{
					this.close();
					choose_close();
				});
			}
			else
			{
				messageAlert(3,"删除失败！",function()
						{
					this.close();
					choose_close();
				});
			}
		}
	});

}

